package com.zhengtoudai.ui.presenter;

import com.zhengtoudai.global.Config;
import com.zhengtoudai.http.HttpApi;
import com.zhengtoudai.injector.PerActivity;
import com.zhengtoudai.ui.bean.Result;
import com.zhengtoudai.ui.view.CommonView;
import com.zhengtoudai.util.DesUtil;

import javax.inject.Inject;

import rx.Subscriber;
import rx.Subscription;
import rx.functions.Action0;

/**
 * Created by Administrator on 2016/8/25 0025.
 */
@PerActivity
public class ForgetPasswordPresenterImpl extends BasePresenterImpl<CommonView> implements ForgetPasswordPresenter {

   @Inject
    public ForgetPasswordPresenterImpl(){

    }

    @Override
    public void forgetPassword(String userPhone, String verityCode, String password, String enterPassword) {
        try {
            password= DesUtil.encrypt(password, Config.DES_KEY);
            enterPassword= DesUtil.encrypt(enterPassword, Config.DES_KEY);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Subscription subscription=HttpApi.getInstance().forgerPassword(userPhone,password,enterPassword,verityCode)
                .doOnSubscribe(new Action0() {
                    @Override
                    public void call() {
                        mView.showDialog();
                    }
                }).subscribe(new Subscriber<Result>() {
                    @Override
                    public void onCompleted() {
                        mView.dismissDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissDialog();
                        mView.onFailure(e);
                    }

                    @Override
                    public void onNext(Result result) {
                        mView.onResult(result);
                    }
                });
        addSubscription(subscription);
    }
}
